package patty.demo;
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/*
 * TRequest URL:http://web.trtc.com.tw/c/2stainfo.asp
 * Request Method:POST
 * Status Code:200 OK
 * Request Headersview source
 * Content-Type:application/x-www-form-urlencoded
 * Host:web.trtc.com.tw
 * Origin:http://web.trtc.com.tw
 * Referer:http://web.trtc.com.tw/c/2stainfo.asp
Form Dataview sourceview URL encoded
s1elect:027
action:query
s2elect:023
submit: 確定
 */

/**
 *
 * @author Patty
 */
public class TaipeiMRT {

  public static void main(String[] args) {
	String from="021", to="029";
    Document doc;
    try {
			Connection connection = Jsoup
					.connect("http://web.trtc.com.tw/c/index_ticket_price.asp");
      connection.data("action","query");
      connection.data("s1elect",from);//起站: 大直
      connection.data("s2elect",to);//迄站: 東湖
      connection.data("submit.x","30");
      connection.data("submit.y","13");
      connection.data("submit","查詢");
      // need http protocol
      doc = connection.userAgent("Mozilla").post();
//      System.out.println(doc);
      
      // get page title
      String title = doc.title();
      System.out.println("title : " + title);

      // 取得路線
      Elements optgroups = doc.select("optgroup");
      String fromString=null, toString=null;
      for (Element opt : optgroups) {
    	  System.out.println("\nopt : " + opt.attr("label") + "**********************");
    	  //System.out.println("\tdata : " + opt.text());
    	  
    	  //取得站名與代號
    	  Elements options = opt.select("option");
          for (Element option : options) {
        	  
        	  System.out.println("\noption : " + option.attr("value"));
        	  System.out.println("\tdata : " + option.text());
        	  if(option.attr("value").equals(from)){
        		  fromString = option.text();
        	  }
        	  if(option.attr("value").equals(to)){
        		  toString = option.text();
        	  }
          }
      }
      
   // 取得站名與代號
//      String fromString=null, toString=null;
//      Elements options = doc.select("option");
//      for (Element option : options) {
//    	  
//    	  System.out.println("\noption : " + option.attr("value"));
//    	  System.out.println("\tdata : " + option.text());
//    	  if(option.attr("value").equals(from)){
//    		  fromString = option.text();
//    	  }
//    	  if(option.attr("value").equals(to)){
//    		  toString = option.text();
//    	  }
//
//      }
      
// 取得票價資料
      Elements divs = doc.select("div");
      for (Element div : divs) {
        // get the value from div attribute
        //System.out.println("\ndiv : " + div.attr("align"));
        //System.out.println("text : " + div.text());
        String data = div.text();
        
        String[] infos = data.split("\\s");
        if(infos.length>=6){
          System.out.printf("**************%n從 [%s] 到 [%s] 的票價：%n", fromString, toString);
          for(String s:infos){
            System.out.println(s);
          }
          break;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
